<template>
  <div class="app-container">
    <div style="width:100%;height:30px;display: flex;
 justify-content: flex-end;
">
      <el-button
          size="mini"
          type="primary" round
          @click="goToBack()"
      >返回上一页
      </el-button>
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <!--<el-form-item label="所属保险id" prop="insureId">-->
      <!--  <el-select v-model="queryParams.insureId" placeholder="请选择所属保险id" clearable>-->
      <!--    <el-option-->
      <!--        v-for="dict in dict.type.sys_user_sex"-->
      <!--        :key="dict.value"-->
      <!--        :label="dict.label"-->
      <!--        :value="dict.value"-->
      <!--    />-->
      <!--  </el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属理赔id" prop="settleId">-->
      <!--  <el-select v-model="queryParams.settleId" placeholder="请选择所属理赔id" clearable>-->
      <!--    <el-option-->
      <!--        v-for="dict in dict.type.sys_user_sex"-->
      <!--        :key="dict.value"-->
      <!--        :label="dict.label"-->
      <!--        :value="dict.value"-->
      <!--    />-->
      <!--  </el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="赔案状态" prop="caseStatus">
        <el-select v-model="queryParams.caseStatus" placeholder="请选择赔案状态" clearable>
          <el-option
              v-for="dict in dict.type.case_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="出险日期">-->
      <!--  <el-date-picker-->
      <!--      v-model="daterangeCaseDate"-->
      <!--      style="width: 300px"-->
      <!--      value-format="yyyy-MM-dd"-->
      <!--      type="daterange"-->
      <!--      range-separator="-"-->
      <!--      start-placeholder="开始日期"-->
      <!--      end-placeholder="结束日期"-->
      <!--  ></el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="报案方式" prop="reportType">
        <el-select v-model="queryParams.reportType" placeholder="请选择报案方式" clearable>
          <el-option
              v-for="dict in dict.type.report_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="报案时间">-->
      <!--  <el-date-picker-->
      <!--      v-model="daterangeReportTime"-->
      <!--      style="width: 240px"-->
      <!--      value-format="yyyy-MM-dd"-->
      <!--      type="daterange"-->
      <!--      range-separator="-"-->
      <!--      start-placeholder="开始日期"-->
      <!--      end-placeholder="结束日期"-->
      <!--  ></el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="报案人" prop="reportPerson">
        <el-input
            v-model="queryParams.reportPerson"
            placeholder="请输入报案人"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="reportTel">
        <el-input
            v-model="queryParams.reportTel"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报案人所属单位" prop="reportCompany" label-width="120px">
        <el-input
            v-model="queryParams.reportCompany"
            placeholder="请输入报案人所属单位"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['insurance:case:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['insurance:case:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['insurance:case:remove']"
        >删除
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
      <!--  <el-button-->
      <!--      type="warning"-->
      <!--      plain-->
      <!--      icon="el-icon-download"-->
      <!--      size="mini"-->
      <!--      @click="handleExport"-->
      <!--      v-hasPermi="['insurance:case:export']"-->
      <!--  >导出-->
      <!--  </el-button>-->
      <!--</el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="caseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="所属保险id" align="center" prop="insureId">-->
      <!--  <template slot-scope="scope">-->
      <!--    <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.insureId"/>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="所属理赔id" align="center" prop="settleId">-->
      <!--  <template slot-scope="scope">-->
      <!--    <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.settleId"/>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <el-table-column label="赔案状态" align="center" prop="caseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.case_status" :value="scope.row.caseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="出险日期" align="center" prop="caseDate" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.caseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出险原因" align="center" prop="reason" :min-width="120"/>
      <el-table-column label="报损金额(元)" align="center" prop="caseAmount" :min-width="120"/>
      <el-table-column label="出险地点" align="center" prop="location" :min-width="100"/>
      <!--<el-table-column label="出险地点经度" align="center" prop="longitude" />-->
      <!--<el-table-column label="出险地点纬度" align="center" prop="latitude" />-->
      <el-table-column label="报案方式" align="center" prop="reportType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_type" :value="scope.row.reportType"/>
        </template>
      </el-table-column>
      <el-table-column label="报案时间" align="center" prop="reportTime" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报案人" align="center" prop="reportPerson"/>
      <el-table-column label="联系电话" align="center" prop="reportTel" :min-width="120"/>
      <el-table-column label="报案人所属单位" align="center" prop="reportCompany" :min-width="120"/>
      <el-table-column label="出险经过" align="center" prop="casePass" :min-width="120"/>
      <!--<el-table-column label="出险现场资料" align="center" prop="material" />-->
      <el-table-column label="施救情况" align="center" prop="saveSituation" :min-width="120"/>
      <el-table-column label="理算过程说明" align="center" prop="describe" :min-width="100"/>
      <el-table-column label="已付赔案金额(元)" align="center" prop="completeMoney" :min-width="130"/>
      <!--<el-table-column label="赔案报告" align="center" prop="report" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              @click="goToPayList(scope.row)"
          >支付信息
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['insurance:case:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color:red"
              @click="handleDelete(scope.row)"
              v-hasPermi="['insurance:case:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
    <!-- 添加或修改保险理赔赔案信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!--<el-card shadow="never" :body-style="{ height: '10px' }" style="margin-bottom: 10px">-->
        <!--  <el-descriptions title="合同主体信息"/>-->
        <!--</el-card>-->
        <el-row>
          <el-col :span="24">
            <el-form-item label="赔案状态" prop="caseStatus">
              <el-select v-model="form.caseStatus" placeholder="请选择赔案状态">
                <el-option
                    v-for="dict in dict.type.case_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="出险日期" prop="caseDate">
              <el-date-picker clearable
                              v-model="form.caseDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              format="yyyy-MM-dd"
                              placeholder="请选择出险日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出险原因" prop="reason">
              <el-input v-model="form.reason" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报损金额(元)" prop="caseAmount">
              <el-input v-model="form.caseAmount" placeholder="请输入报损金额(元)">
                <template slot="append">
                  <el-button slot="append">元</el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出险地点" prop="location">
              <el-input v-model="form.location" type="textarea" placeholder="请输入内容">
                <template slot="append">
                  <el-button slot="append" @click="openMap"> 地图</el-button>
                </template>
              </el-input>
            </el-form-item>
            <!--<el-form-item label="出险地点经度" prop="longitude">-->
            <!--  <el-input v-model="form.longitude" placeholder="请输入出险地点经度"/>-->
            <!--</el-form-item>-->
            <!--<el-form-item label="出险地点纬度" prop="latitude">-->
            <!--  <el-input v-model="form.latitude" placeholder="请输入出险地点纬度"/>-->
            <!--</el-form-item>-->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报案方式" prop="reportType">
              <el-select v-model="form.reportType" placeholder="请选择报案方式">
                <el-option
                    v-for="dict in dict.type.report_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报案时间" prop="reportTime">
              <el-date-picker clearable
                              v-model="form.reportTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              format="yyyy-MM-dd"
                              placeholder="请选择报案时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报案人" prop="reportPerson">
              <el-input v-model="form.reportPerson" placeholder="请输入报案人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="reportTel">
              <el-input v-model="form.reportTel" placeholder="请输入联系电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="报案人所属单位" prop="reportCompany">
              <el-input v-model="form.reportCompany" placeholder="请输入报案人所属单位"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="24">

            <el-form-item label="出险经过" prop="casePass">
              <el-input v-model="form.casePass" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="出险现场资料" prop="material">
              <file-upload v-model="form.material"  :fileType="['pdf']" :fileSize="9999" :limit="99999"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="施救情况" prop="saveSituation">
              <el-input v-model="form.saveSituation" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="理算过程说明" prop="describe">
              <el-input v-model="form.describe" type="textarea" placeholder="请输入理算过程说明"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="已付赔案金额" prop="completeMoney">
              <el-input v-model="form.completeMoney" readonly >
                <template slot="append">
                  <el-button slot="append">元</el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="赔案报告" prop="report">
              <file-upload v-model="form.report" :fileType="['pdf']" :fileSize="99999" :limit="99999"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCase, getCase, delCase, addCase, updateCase} from "@/api/insurance/case";
import {isEmpty, isValidAmount} from "@/utils/validate";

export default {
  name: "Case",
  dicts: ['report_type', 'case_status', 'sys_user_sex'],
  data() {
    const checkCompleteMoney = (rule, value, callback) => {
      if (isEmpty(value)) {
        callback();
        return
      }
      if (!isValidAmount(value)) {
        callback(new Error('请输入正确的金额'));
        return;
      }
      callback();
    }
    const checkCaseAmount = (rule, value, callback) => {
      if (isEmpty(value)) {
        callback();
        return
      }
      if (!isValidAmount(value)) {
        callback(new Error('请输入正确的金额'));
        return;
      }
      callback();
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险理赔赔案信息表格数据
      caseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 时间范围
      daterangeCaseDate: [],
      // 时间范围
      daterangeReportTime: [],
      insureId: null,
      settleId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insureId: null,
        settleId: null,
        caseStatus: null,
        caseDate: null,
        reason: null,
        caseAmount: null,
        location: null,
        longitude: null,
        latitude: null,
        reportType: null,
        reportTime: null,
        reportPerson: null,
        reportTel: null,
        reportCompany: null,
        casePass: null,
        material: null,
        saveSituation: null,
        describe: null,
        completeMoney: null,
        report: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        completeMoney: [
          // {required: true, message: "保险费率不能为空", trigger: "blur"},
          {validator: checkCompleteMoney, trigger: 'blur'}
        ],
        caseAmount: [
          {validator: checkCaseAmount, trigger: 'blur'}
        ]
      }
    };
  },
  created() {
    this.settleId = this.$route.query.settleId
    const insureId = this.$route.params && this.$route.params.insureId;
    if (isEmpty(insureId)) {
      this.$modal.msgWarning('请重新进入列表页面');
      return
    }
    this.queryParams.insureId = insureId || this.queryParams
    this.insureId = insureId
    this.getList();
  },
  methods: {
    openMap(){
      this.$modal.msg('打开地图')
    },
    goToBack(){
      this.$router.push({path: "/lpxx/settle" });
    },
    /** 查询保险理赔赔案信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCaseDate && '' != this.daterangeCaseDate) {
        this.queryParams.params["beginCaseDate"] = this.daterangeCaseDate[0];
        this.queryParams.params["endCaseDate"] = this.daterangeCaseDate[1];
      }
      if (null != this.daterangeReportTime && '' != this.daterangeReportTime) {
        this.queryParams.params["beginReportTime"] = this.daterangeReportTime[0];
        this.queryParams.params["endReportTime"] = this.daterangeReportTime[1];
      }
      listCase(this.queryParams).then(response => {
        this.caseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        insureId: null,
        settleId: null,
        caseStatus: null,
        caseDate: null,
        reason: null,
        caseAmount: null,
        location: null,
        longitude: null,
        latitude: null,
        reportType: null,
        reportTime: null,
        reportPerson: null,
        reportTel: null,
        reportCompany: null,
        casePass: null,
        material: null,
        saveSituation: null,
        describe: null,
        completeMoney: null,
        report: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        update: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCaseDate = [];
      this.daterangeReportTime = [];
      this.resetForm("queryForm");
      this.queryParams.insureId = this.insureId
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保险理赔赔案信息";
    },
    goToPayList(row) {
      // 支付列表页面
      this.$router.push({
        path: "/insurance/pay/index",
        query: {
          "settleId": row.settleId,
          "insureId": row.insureId,
          "caseId":row.id
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCase(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保险理赔赔案信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.insureId = this.insureId
          this.form.settleId = this.settleId
          if (this.form.id != null) {
            updateCase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选择的保险理赔赔案信息？').then(function () {
        return delCase(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('insurance/case/export', {
        ...this.queryParams
      }, `case_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
